package com.longdaji.decoration.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.bean.AddComment;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Kanade on 2018/4/30 0030.
 */

public class GoodsCommentAdapter extends RecyclerView.Adapter<GoodsCommentAdapter.ViewHolder> {
    private List<AddComment> mAddCommentList;
    private Context mContext;

    private List<ViewHolder> mHolderList = new ArrayList<>();

    public GoodsCommentAdapter(List<AddComment> list) {
        this.mAddCommentList = list;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_goods_pic)
        ImageView ivGoodsPic;
        @BindView(R.id.iv_high_comment)
        ImageView ivHighComment;
        @BindView(R.id.iv_medium_comment)
        ImageView ivMediumComment;
        @BindView(R.id.iv_negative_comment)
        ImageView ivNegativeComment;
        @BindView(R.id.iv_anonymous)
        ImageView ivAnonymous;
        @BindView(R.id.rv_photo)
        RecyclerView rvPhoto;
        @BindView(R.id.et_comment_content)
        EditText etCommentContent;

        private List<Uri> mUriList = new ArrayList<>();
        private PhotoAdapter adapter = new PhotoAdapter(mUriList);

        private boolean isHighComment, isMediumComment, isNegativeComment;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


        @OnClick({R.id.ll_high_comment, R.id.ll_medium_comment, R.id.ll_negative_comment, R.id.iv_anonymous})
        void onItemViewClicked(View view) {
            switch (view.getId()) {
                case R.id.ll_high_comment:
                    mCommentListener.onHighCommentClick(getAdapterPosition());
                    chooseLevel(Constants.HIGH);
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.HIGH);
                    break;
                case R.id.ll_medium_comment:
                    mCommentListener.onMediumCommentClick(getAdapterPosition());
                    chooseLevel(Constants.MEDIUM);
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.MEDIUM);
                    break;
                case R.id.ll_negative_comment:
                    mCommentListener.onNegativeCommentClick(getAdapterPosition());
                    chooseLevel(Constants.NEGATIVE);
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.NEGATIVE);
                    break;
                case R.id.iv_anonymous:
                    mCommentListener.onAnonymousClick(getAdapterPosition());
                    if (!mAddCommentList.get(getAdapterPosition()).isAnonymous()) {
                        ivAnonymous.setImageResource(R.mipmap.iv_checked);
                        mAddCommentList.get(getAdapterPosition()).setAnonymous(true);
                    } else {
                        ivAnonymous.setImageResource(R.mipmap.iv_unchecked);
                        mAddCommentList.get(getAdapterPosition()).setAnonymous(false);
                    }
                    break;
                default:
                    break;
            }
        }

        public void setItemContent() {
            mAddCommentList.get(getAdapterPosition()).setCommentContent(etCommentContent.getText().toString());
        }

        private void resetChosenComment() {
            if (mAddCommentList.get(getAdapterPosition()).getCommentLevel() == Constants.HIGH) {
                ivHighComment.setImageResource(R.mipmap.high_comment);
            } else if (mAddCommentList.get(getAdapterPosition()).getCommentLevel() == Constants.MEDIUM) {
                ivMediumComment.setImageResource(R.mipmap.medium_comment);
            } else if (mAddCommentList.get(getAdapterPosition()).getCommentLevel() == Constants.NEGATIVE) {
                ivNegativeComment.setImageResource(R.mipmap.negative_comment);
            }
        }

        private void chooseLevel(int level) {
            resetChosenComment();
            switch (level) {
                case Constants.HIGH:
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.HIGH);
                    ivHighComment.setImageResource(R.mipmap.high_comment_after);
                    break;
                case Constants.MEDIUM:
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.MEDIUM);
                    ivMediumComment.setImageResource(R.mipmap.medium_comment_after);
                    break;
                case Constants.NEGATIVE:
                    mAddCommentList.get(getAdapterPosition()).setCommentLevel(Constants.NEGATIVE);
                    ivNegativeComment.setImageResource(R.mipmap.negative_comment_after);
                    break;
                default:
                    break;
            }
        }

//        public void setPhoto(List<Uri> uriList) {
//            mUriList.clear();
//            mUriList.addAll(uriList);
//            notifyDataSetChanged();
//        }

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mContext == null) {
            mContext = parent.getContext();
        }
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_comment, parent, false);
        ViewHolder holder = new ViewHolder(view);
        mHolderList.add(holder);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        AddComment addComment = mAddCommentList.get(position);
        Glide.with(mContext).load(addComment.getGoodsImage()).into(holder.ivGoodsPic);
        holder.rvPhoto.setLayoutManager(new GridLayoutManager(mContext, 4));
        holder.adapter.setData(addComment.getCommentImages());
        holder.adapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
            @Override
            public void addPhoto() {
                mCommentListener.onCreateDialogClick(holder.getAdapterPosition());
            }
        });
        holder.rvPhoto.setAdapter(holder.adapter);
        if (addComment.getCommentLevel() == Constants.HIGH) {
            holder.chooseLevel(Constants.HIGH);
        } else if (addComment.getCommentLevel() == Constants.MEDIUM) {
            holder.chooseLevel(Constants.MEDIUM);
        } else if (addComment.getCommentLevel() == Constants.NEGATIVE) {
            holder.chooseLevel(Constants.NEGATIVE);
        } else {
            LogUtil.e("comment level error");
        }
        if (addComment.isAnonymous()) {
            Glide.with(mContext).load(R.mipmap.iv_checked).into(holder.ivAnonymous);
        } else {
            Glide.with(mContext).load(R.mipmap.iv_unchecked).into(holder.ivAnonymous);
        }
//        String content = holder.etCommentContent.getText().toString();
//        addComment.setCommentContent(content);

    }

    @Override
    public int getItemCount() {
        return mAddCommentList.size();
    }

    private CommentListener mCommentListener;

    public interface CommentListener {
        void onHighCommentClick(int position);

        void onMediumCommentClick(int position);

        void onNegativeCommentClick(int position);

        void onAnonymousClick(int position);

        void onCreateDialogClick(int position);
    }

    public void setCommentListener(CommentListener listener) {
        this.mCommentListener = listener;
    }


    public void setData(List<AddComment> list) {
        mAddCommentList.clear();
        mAddCommentList.addAll(list);
        notifyDataSetChanged();
    }

    public void setContent() {
        for (ViewHolder holder : mHolderList) {
            if (holder != null) {
                holder.setItemContent();
            }
        }
    }

//    private List<Orders.OrdersBean.OrderBean.ProductsBean> mAddCommentList;
//    private Context mContext;
//
//    public GoodsCommentAdapter(List<Orders.OrdersBean.OrderBean.ProductsBean> list) {
//        this.mAddCommentList = list;
//    }
//
//    class ViewHolder extends RecyclerView.ViewHolder {
//        @BindView(R.id.iv_goods_pic)
//        ImageView ivGoodsPic;
//        @BindView(R.id.iv_high_comment)
//        ImageView ivHighComment;
//        @BindView(R.id.iv_medium_comment)
//        ImageView ivMediumComment;
//        @BindView(R.id.iv_negative_comment)
//        ImageView ivNegativeComment;
//        @BindView(R.id.iv_anonymous)
//        ImageView ivAnonymous;
//        @BindView(R.id.rv_photo)
//        RecyclerView rvPhoto;
//
//        private List<Uri> mUriList = new ArrayList<>();
//        private PhotoAdapter adapter = new PhotoAdapter(mUriList);
//
//        private boolean isHighComment, isMediumComment, isNegativeComment;
//        private boolean isAnonymous;
//
//        public ViewHolder(View itemView) {
//            super(itemView);
//            ButterKnife.bind(this, itemView);
//        }
//
//        @OnClick({R.id.ll_high_comment, R.id.ll_medium_comment, R.id.ll_negative_comment, R.id.iv_anonymous})
//        void onItemViewClicked(View view) {
//            switch (view.getId()) {
//                case R.id.ll_high_comment:
//                    mCommentListener.onHighCommentClick(getAdapterPosition());
//                    refreshChosenComment();
//                    isHighComment = true;
//                    ivHighComment.setImageResource(R.mipmap.high_comment_after);
//                    break;
//                case R.id.ll_medium_comment:
//                    mCommentListener.onMediumCommentClick(getAdapterPosition());
//                    refreshChosenComment();
//                    isMediumComment = true;
//                    ivMediumComment.setImageResource(R.mipmap.medium_comment_after);
//                    break;
//                case R.id.ll_negative_comment:
//                    mCommentListener.onNegativeCommentClick(getAdapterPosition());
//                    refreshChosenComment();
//                    isNegativeComment = true;
//                    ivNegativeComment.setImageResource(R.mipmap.negative_comment_after);
//                    break;
//                case R.id.iv_anonymous:
//                    mCommentListener.onAnonymousClick(getAdapterPosition());
//                    if (!isAnonymous) {
//                        isAnonymous = true;
//                        ivAnonymous.setImageResource(R.mipmap.iv_checked);
//                    } else {
//                        isAnonymous = false;
//                        ivAnonymous.setImageResource(R.mipmap.iv_unchecked);
//                    }
//                    break;
//                default:
//                    break;
//            }
//        }
//
//        private void refreshChosenComment() {
//            if (isHighComment) {
//                isHighComment = false;
//                ivHighComment.setImageResource(R.mipmap.high_comment);
//            }
//            if (isMediumComment) {
//                isMediumComment = false;
//                ivMediumComment.setImageResource(R.mipmap.medium_comment);
//            }
//            if (isNegativeComment) {
//                isNegativeComment = false;
//                ivNegativeComment.setImageResource(R.mipmap.negative_comment);
//            }
//        }
//
//        public void setPhoto(List<Uri> uriList) {
//            mUriList.clear();
//            mUriList.addAll(uriList);
//            notifyDataSetChanged();
//        }
//
//    }
//
//    @Override
//    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        if (mContext == null) {
//            mContext = parent.getContext();
//        }
//        View view = LayoutInflater.from(mContext).inflate(R.layout.item_goods_comment, parent, false);
//        return new ViewHolder(view);
//    }
//
//    @Override
//    public void onBindViewHolder(final ViewHolder holder, int position) {
//        Orders.OrdersBean.OrderBean.ProductsBean product = mAddCommentList.get(position);
//        if (product.getGoodsSkuImg() != null) {
//            Glide.with(mContext).load(product.getGoodsSkuImg()).into(holder.ivGoodsPic);
//        }
//        List<Uri> mUriList = new ArrayList<>();
//        PhotoAdapter adapter = new PhotoAdapter(mUriList);
//
//        holder.rvPhoto.setLayoutManager(new GridLayoutManager(mContext, 4));
//        holder.adapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
//            @Override
//            public void addPhoto() {
//                mCommentListener.onCreateDialogClick(holder.getAdapterPosition());
//            }
//        });
//        holder.rvPhoto.setAdapter(holder.adapter);
//    }
//
//    @Override
//    public int getItemCount() {
//        return mAddCommentList.size();
//    }
//
//    private CommentListener mCommentListener;
//
//    public interface CommentListener {
//        void onHighCommentClick(int position);
//
//        void onMediumCommentClick(int position);
//
//        void onNegativeCommentClick(int position);
//
//        void onAnonymousClick(int position);
//
//        void onCreateDialogClick(int position);
//    }
//
//    public void setCommentListener(CommentListener listener) {
//        this.mCommentListener = listener;
//    }
//
//
//    public void setData(List<Orders.OrdersBean.OrderBean.ProductsBean> list) {
//        mAddCommentList.clear();
//        mAddCommentList.addAll(list);
//        notifyDataSetChanged();
//    }


}
